{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 04 分类准确度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits = datasets.load_digits()\n",
    "digits.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optical Recognition of Handwritten Digits Data Set\n",
      "===================================================\n",
      "\n",
      "Notes\n",
      "-----\n",
      "Data Set Characteristics:\n",
      "    :Number of Instances: 5620\n",
      "    :Number of Attributes: 64\n",
      "    :Attribute Information: 8x8 image of integer pixels in the range 0..16.\n",
      "    :Missing Attribute Values: None\n",
      "    :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n",
      "    :Date: July; 1998\n",
      "\n",
      "This is a copy of the test set of the UCI ML hand-written digits datasets\n",
      "http://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\n",
      "\n",
      "The data set contains images of hand-written digits: 10 classes where\n",
      "each class refers to a digit.\n",
      "\n",
      "Preprocessing programs made available by NIST were used to extract\n",
      "normalized bitmaps of handwritten digits from a preprinted form. From a\n",
      "total of 43 people, 30 contributed to the training set and different 13\n",
      "to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n",
      "4x4 and the number of on pixels are counted in each block. This generates\n",
      "an input matrix of 8x8 where each element is an integer in the range\n",
      "0..16. This reduces dimensionality and gives invariance to small\n",
      "distortions.\n",
      "\n",
      "For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\n",
      "T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\n",
      "L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\n",
      "1994.\n",
      "\n",
      "References\n",
      "----------\n",
      "  - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\n",
      "    Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\n",
      "    Graduate Studies in Science and Engineering, Bogazici University.\n",
      "  - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n",
      "  - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n",
      "    Linear dimensionalityreduction using relevance weighted LDA. School of\n",
      "    Electrical and Electronic Engineering Nanyang Technological University.\n",
      "    2005.\n",
      "  - Claudio Gentile. A New Approximate Maximal Margin Classification\n",
      "    Algorithm. NIPS. 2000.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(digits.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = digits.data\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = digits.target\n",
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,\n",
       "       3, 4, 5, 6, 7, 8, 9, 0, 9, 5, 5, 6, 5, 0, 9, 8, 9, 8, 4, 1, 7, 7, 3,\n",
       "       5, 1, 0, 0, 2, 2, 7, 8, 2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4, 6, 6, 6, 4,\n",
       "       9, 1, 5, 0, 9, 5, 2, 8, 2, 0, 0, 1, 7, 6, 3, 2, 1, 7, 4, 6, 3, 1, 3,\n",
       "       9, 1, 7, 6, 8, 4, 3, 1])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   0.,   5.,  13.,   9.,   1.,   0.,   0.,   0.,   0.,  13.,\n",
       "         15.,  10.,  15.,   5.,   0.,   0.,   3.,  15.,   2.,   0.,  11.,\n",
       "          8.,   0.,   0.,   4.,  12.,   0.,   0.,   8.,   8.,   0.,   0.,\n",
       "          5.,   8.,   0.,   0.,   9.,   8.,   0.,   0.,   4.,  11.,   0.,\n",
       "          1.,  12.,   7.,   0.,   0.,   2.,  14.,   5.,  10.,  12.,   0.,\n",
       "          0.,   0.,   0.,   6.,  13.,  10.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,  12.,  13.,   5.,   0.,   0.,   0.,   0.,   0.,\n",
       "         11.,  16.,   9.,   0.,   0.,   0.,   0.,   3.,  15.,  16.,   6.,\n",
       "          0.,   0.,   0.,   7.,  15.,  16.,  16.,   2.,   0.,   0.,   0.,\n",
       "          0.,   1.,  16.,  16.,   3.,   0.,   0.,   0.,   0.,   1.,  16.,\n",
       "         16.,   6.,   0.,   0.,   0.,   0.,   1.,  16.,  16.,   6.,   0.,\n",
       "          0.,   0.,   0.,   0.,  11.,  16.,  10.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   4.,  15.,  12.,   0.,   0.,   0.,   0.,   3.,\n",
       "         16.,  15.,  14.,   0.,   0.,   0.,   0.,   8.,  13.,   8.,  16.,\n",
       "          0.,   0.,   0.,   0.,   1.,   6.,  15.,  11.,   0.,   0.,   0.,\n",
       "          1.,   8.,  13.,  15.,   1.,   0.,   0.,   0.,   9.,  16.,  16.,\n",
       "          5.,   0.,   0.,   0.,   0.,   3.,  13.,  16.,  16.,  11.,   5.,\n",
       "          0.,   0.,   0.,   0.,   3.,  11.,  16.,   9.,   0.],\n",
       "       [  0.,   0.,   7.,  15.,  13.,   1.,   0.,   0.,   0.,   8.,  13.,\n",
       "          6.,  15.,   4.,   0.,   0.,   0.,   2.,   1.,  13.,  13.,   0.,\n",
       "          0.,   0.,   0.,   0.,   2.,  15.,  11.,   1.,   0.,   0.,   0.,\n",
       "          0.,   0.,   1.,  12.,  12.,   1.,   0.,   0.,   0.,   0.,   0.,\n",
       "          1.,  10.,   8.,   0.,   0.,   0.,   8.,   4.,   5.,  14.,   9.,\n",
       "          0.,   0.,   0.,   7.,  13.,  13.,   9.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   1.,  11.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          7.,   8.,   0.,   0.,   0.,   0.,   0.,   1.,  13.,   6.,   2.,\n",
       "          2.,   0.,   0.,   0.,   7.,  15.,   0.,   9.,   8.,   0.,   0.,\n",
       "          5.,  16.,  10.,   0.,  16.,   6.,   0.,   0.,   4.,  15.,  16.,\n",
       "         13.,  16.,   1.,   0.,   0.,   0.,   0.,   3.,  15.,  10.,   0.,\n",
       "          0.,   0.,   0.,   0.,   2.,  16.,   4.,   0.,   0.],\n",
       "       [  0.,   0.,  12.,  10.,   0.,   0.,   0.,   0.,   0.,   0.,  14.,\n",
       "         16.,  16.,  14.,   0.,   0.,   0.,   0.,  13.,  16.,  15.,  10.,\n",
       "          1.,   0.,   0.,   0.,  11.,  16.,  16.,   7.,   0.,   0.,   0.,\n",
       "          0.,   0.,   4.,   7.,  16.,   7.,   0.,   0.,   0.,   0.,   0.,\n",
       "          4.,  16.,   9.,   0.,   0.,   0.,   5.,   4.,  12.,  16.,   4.,\n",
       "          0.,   0.,   0.,   9.,  16.,  16.,  10.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,  12.,  13.,   0.,   0.,   0.,   0.,   0.,   5.,\n",
       "         16.,   8.,   0.,   0.,   0.,   0.,   0.,  13.,  16.,   3.,   0.,\n",
       "          0.,   0.,   0.,   0.,  14.,  13.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,  15.,  12.,   7.,   2.,   0.,   0.,   0.,   0.,  13.,  16.,\n",
       "         13.,  16.,   3.,   0.,   0.,   0.,   7.,  16.,  11.,  15.,   8.,\n",
       "          0.,   0.,   0.,   1.,   9.,  15.,  11.,   3.,   0.],\n",
       "       [  0.,   0.,   7.,   8.,  13.,  16.,  15.,   1.,   0.,   0.,   7.,\n",
       "          7.,   4.,  11.,  12.,   0.,   0.,   0.,   0.,   0.,   8.,  13.,\n",
       "          1.,   0.,   0.,   4.,   8.,   8.,  15.,  15.,   6.,   0.,   0.,\n",
       "          2.,  11.,  15.,  15.,   4.,   0.,   0.,   0.,   0.,   0.,  16.,\n",
       "          5.,   0.,   0.,   0.,   0.,   0.,   9.,  15.,   1.,   0.,   0.,\n",
       "          0.,   0.,   0.,  13.,   5.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   9.,  14.,   8.,   1.,   0.,   0.,   0.,   0.,  12.,\n",
       "         14.,  14.,  12.,   0.,   0.,   0.,   0.,   9.,  10.,   0.,  15.,\n",
       "          4.,   0.,   0.,   0.,   3.,  16.,  12.,  14.,   2.,   0.,   0.,\n",
       "          0.,   4.,  16.,  16.,   2.,   0.,   0.,   0.,   3.,  16.,   8.,\n",
       "         10.,  13.,   2.,   0.,   0.,   1.,  15.,   1.,   3.,  16.,   8.,\n",
       "          0.,   0.,   0.,  11.,  16.,  15.,  11.,   1.,   0.],\n",
       "       [  0.,   0.,  11.,  12.,   0.,   0.,   0.,   0.,   0.,   2.,  16.,\n",
       "         16.,  16.,  13.,   0.,   0.,   0.,   3.,  16.,  12.,  10.,  14.,\n",
       "          0.,   0.,   0.,   1.,  16.,   1.,  12.,  15.,   0.,   0.,   0.,\n",
       "          0.,  13.,  16.,   9.,  15.,   2.,   0.,   0.,   0.,   0.,   3.,\n",
       "          0.,   9.,  11.,   0.,   0.,   0.,   0.,   0.,   9.,  15.,   4.,\n",
       "          0.,   0.,   0.,   9.,  12.,  13.,   3.,   0.,   0.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "some_digit = X[666]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "some_digit_image = some_digit.reshape(8, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACuxJREFUeJzt3d2LXeUZhvH77qi0qTYDTVokid1BSkAKncgmICkmjVhi\nFc1BDxJQSCjkSFFaEO2R/QckPSiCRCeCidJGDSJWsehohdY6iZPWfFjSMCUTtJlQBj8OGqJPD2YF\noqTsNdnrax6uH4TOx2a/z6a5XGv2rKzXESEAOX2t7QEA1IfAgcQIHEiMwIHECBxIjMCBxAgcSIzA\ngcQIHEjsijqedNmyZdHr9ep46lbNzc01ut709HRja42MjDS21vXXX9/YWkuWLGlsrSZNT0/r7Nmz\nHvS4WgLv9XqanJys46lbdeDAgUbX2759e2NrjY6ONrbWvn37GltrbGyssbWa1O/3Sz2OU3QgMQIH\nEiNwIDECBxIjcCAxAgcSI3AgMQIHEisVuO3Ntj+wfcL2Q3UPBaAaAwO3PSLpt5Juk3SDpG22b6h7\nMADDK3MEXyfpREScjIhzkp6VdFe9YwGoQpnAV0g6ddHnM8XX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q/9KiJebnEmDHafpL3FweakpB0tzzOU1n9NBqA+XThFB1ATAgcSI3Ag\nMQIHEiNwIDECBxIjcCAxAgcS+x9nOLNnrGWrWgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x116344a20>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(some_digit_image, cmap = matplotlib.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from playML.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_ratio=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from playML.kNN import KNNClassifier\n",
    "\n",
    "my_knn_clf = KNNClassifier(k=3)\n",
    "my_knn_clf.fit(X_train, y_train)\n",
    "y_predict = my_knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.99442896935933145"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test) / len(y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 封装我们自己的accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.99442896935933145"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from playML.metrics import accuracy_score\n",
    "\n",
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.99442896935933145"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### scikit-learn中的accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "knn_clf = KNeighborsClassifier(n_neighbors=3)\n",
    "knn_clf.fit(X_train, y_train)\n",
    "y_predict = knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.98888888888888893"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.98888888888888893"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test, y_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
